using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;

namespace marbletrack
{
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class MouseSquare
    {
        private VertexPositionNormalTexture[] pointList = new VertexPositionNormalTexture[4];
        private int[] lineListIndices = new int[] { 0, 1, 3, 1, 2, 3,3,2,1,3,1,0};
        //public Vector3 squareColour = new Vector3(255, 0, 0);
        public float hieght = 0;
        private Vector3 vectorZero;
        public Vector3 Velocity = Vector3.Zero;
        public Texture2D texture_;
        protected int frameCount = 0;
        protected float size = Game1.HALFSQUARESIZE;

        VertexDeclaration vertexDeclaration;

        public MouseSquare()
        {
            Position = Vector3.Zero;

            //Vertex Declaration
            vertexDeclaration = new VertexDeclaration(Game1.Graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
        }

        public MouseSquare(Vector3 position, Texture2D texture)
        {
            Position = position;
            texture_ = texture;

            //Vertex Declaration
            vertexDeclaration = new VertexDeclaration(Game1.Graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
        }

        public Vector3 Position
        {
            get
            {
                return vectorZero;
            }
            set
            {
                vectorZero = value;

                Vector3 vector1 = vectorZero + ((Vector3.UnitX * -size) + (Vector3.UnitZ * size));
                Vector3 vector2 = vectorZero + ((Vector3.UnitX * -size) + (Vector3.UnitZ * -size));
                Vector3 vector3 = vectorZero + ((Vector3.UnitX * size) + (Vector3.UnitZ * -size));
                Vector3 vector4 = vectorZero + ((Vector3.UnitX * size) + (Vector3.UnitZ * size));

                pointList[0] = new VertexPositionNormalTexture(vector1, Vector3.Forward, new Vector2(0, 1));
                pointList[1] = new VertexPositionNormalTexture(vector2, Vector3.Forward, new Vector2(0, 0));
                pointList[2] = new VertexPositionNormalTexture(vector3, Vector3.Forward, new Vector2(1, 0));
                pointList[3] = new VertexPositionNormalTexture(vector4, Vector3.Forward, new Vector2(1, 1));
            }
        }

        public void Update()
        {
            //only used for when this class was used as an animation
            if (frameCount <20)
            {
                Position = vectorZero + Velocity;
            }
            else
            {
                //frameCount = 0;
                Position = vectorZero - Velocity;
            }
            frameCount++;
        }

        public void Draw()
        {
            Game1.Graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
            
            //Game1.basicEffect.DiffuseColor = squareColour;
            Game1.basicEffect.View = Game1.view;
            Game1.basicEffect.Projection = Game1.projectionMatrix;
            Game1.basicEffect.Texture = texture_;
                Game1.basicEffect.Begin();

                foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList, pointList, 0, 4, lineListIndices, 0, 4);
                    pass.End();
                }
                Game1.basicEffect.End();
        }

    }
}